package com.p7.notepad.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.p7.notepad.R;
import com.p7.notepad.activity.AddFileActivity;
import com.p7.notepad.activity.LoginActivity;
import com.p7.notepad.activity.NotepadListActivity;
import com.p7.notepad.adapter.FileAdapter;
import com.p7.notepad.app.BaseFragment;
import com.p7.notepad.app.LoadMoreHelper;
import com.p7.notepad.bean.File;
import com.p7.notepad.bean.Success;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;
import com.p7.notepad.tools.Storage;
import com.tools.s7.recycler.DividerItemDecoration;
import com.tools.s7.recycler.listener.Recycler;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class FileFragment extends BaseFragment implements LoadMoreHelper.LoadDataListener,
        Recycler.OnClickListener<File.DataBean> {

    @BindView(R.id.file_srl)
    SwipeRefreshLayout mSrl;
    @BindView(R.id.file_data)
    RecyclerView mData;
    @BindView(R.id.file_hint_text)
    TextView mHintText;
    @BindView(R.id.file_hint)
    LinearLayout mHint;

    private LoadMoreHelper mHelper;

    public FileFragment() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_file;
    }

    @Override
    protected void initEvents() {
        super.initEvents();

        mHelper = new LoadMoreHelper.Builder<File.DataBean>()
                .setPageSize(20)
                .setSwipeRefresh(mSrl)
                .setRecycler(mData, new LinearLayoutManager(getActivity()), new DividerItemDecoration(getActivity(), 1, 0xFFDFDFDF))
                .setAdapter(new FileAdapter(), false)
                .setLoadData(this)
                .setClick(this)
                .build();
    }

    @Override
    public void onResume() {
        super.onResume();
        mHelper.loadFirstPage();
    }

    @OnClick({R.id.file_add, R.id.file_hint_btn, R.id.file_hint})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.file_add:
                startActivity(AddFileActivity.class);
                break;
            case R.id.file_hint_btn:
                mHint.setVisibility(View.GONE);
                onDelete();
                break;
            case R.id.file_hint:
                mHint.setVisibility(View.GONE);
                break;
        }
    }

    private File.DataBean delData;
    @Override
    public void onItemClick(View view, int i, File.DataBean dataBean) {
        switch (view.getId()) {
            case R.id.file_btn:
                Bundle bundle = new Bundle();
                bundle.putString("title", dataBean.getName());
                bundle.putString("id", String.valueOf(dataBean.getId()));
                startActivity(NotepadListActivity.class, bundle);
                break;
            case R.id.file_del:
                delData = dataBean;
                mHint.setVisibility(View.VISIBLE);
                mHintText.setText("确认删除--" + delData.getName() + "?");
                break;
        }
    }

    @Override
    public void onLoadData(final int i) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("userId", String.valueOf(Storage.getId()));
        parameters.put("pageNum", String.valueOf(i));
        parameters.put("pageSize", String.valueOf(20));
        APIManager.onPost("folder/getFolder", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                File file = gson.fromJson(s, File.class);
                if (file.getCode() == 1000) {
                    mHelper.notifyAdapter(file.getData());
                } else if (file.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                } else {
                    if (i == 1) {
                        mHelper.notifyNull();
                    } else {
                        mHelper.notifyError();
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                mHelper.notifyError();
            }
        });
    }

    private void onDelete() {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", String.valueOf(delData.getId()));
        APIManager.onPost("folder/delFolder", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                Success success = gson.fromJson(s, Success.class);
                if (success.getCode() == 1000) {
                   mHelper.loadFirstPage();
                   setToast("删除成功");
                } else if (success.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                } else {
                    setToast(success.getMessage() + "请稍后重试");
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                mHelper.notifyError();
            }
        });
    }

    private void setToast(String str) {
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }
}
